#pragma once

#include <sys/epoll.h>
#include <unistd.h>
#include <iostream>

enum {
    EPOLL_CREATE_ERR,
};

class Epoller
{
    static const int size = 128;

public:
    Epoller()
    {
        _epfd = epoll_create(size);
        if(_epfd < 0) {
            std::cerr << "epoll create err" << std::endl;
            exit(EPOLL_CREATE_ERR);
        }
        else { std::cerr << "epoll create done" << std::endl; }
    }

    int EpollerWait(struct epoll_event events[], int n, int timeout)
    {
        int ret = epoll_wait(_epfd, events, n, timeout);
        return ret;
    }

    void EpollerUpdate(int op, int fd, uint32_t event)
    {
        if(op == EPOLL_CTL_DEL) {
            epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, nullptr);
        }
        else
        {
            struct epoll_event evnt;
            evnt.events = event;
            evnt.data.fd = fd;
            epoll_ctl(_epfd, op, fd, &evnt);
        }
    }

    ~Epoller()
    {
        if(_epfd >= 0) {
            close(_epfd);
        }
    }

private:
    int _epfd;
};